package com.newenergy.wisehome.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.config.AppManager;
import com.newenergy.wisehome.data.bean.BallLight;
import com.newenergy.wisehome.data.bean.DeviceCategory;
import com.newenergy.wisehome.data.bean.Family;
import com.newenergy.wisehome.data.ormlite.dao.BallLightDao;
import com.newenergy.wisehome.data.ormlite.dao.DeviceCategoryDao;
import com.newenergy.wisehome.ui.adapter.DeviceAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.ISupportFragment;

/**
 * 设备列表
 * Created by new on 2017/11/30.
 */

public class DeviceFragment extends BaseFragment{

    private static String ARG_DEVICE="arg_device";
    @BindView(R.id.btn_back)
    Button mBtnBack;
    @BindView(R.id.tv_back)
    TextView mTvBack;
    @BindView(R.id.tv_page)
    TextView mTvPage;
    @BindView(R.id.recyclerView)
    EasyRecyclerView mRecyclerView;

    private DeviceAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;

    private Family mCurrFamily;
    private int mGroupPosition = 0;
    private DeviceCategory mGroup;
    private int confirmType = 1;

    private BallLight mBallLight;
   /* private Fan mFan;
    private FanLamp mFanLamp;
    private ElectricHeater mElectricHeater;
    private BathHeater mBathHeater;
    private Curtain mCurtain;
    private Clotheshorse mClotheshorse;
    private AirConditioning mAirConditioning;
    private HealthBasin mHealthBasinp;*/

    public DeviceFragment() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_device_list;
    }


    public static ISupportFragment newInstace(Family currFamily) {
        Bundle args = new Bundle();
        args.putParcelable(ARG_DEVICE, currFamily);
        DeviceFragment fragment = new DeviceFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void init(View rootView) {
        initRecycleView();

    }

    @Override
    public void onResume() {
        super.onResume();
        mCurrFamily = getArguments().getParcelable(ARG_DEVICE);
        if (mCurrFamily==null){
            mTvPage.setText(getString(R.string.no_family));
            return;
        }
        mTvPage.setText(mCurrFamily.getName());
        DeviceCategoryDao deviceCategoryDao = new DeviceCategoryDao(getContext());
        List<DeviceCategory> groups = deviceCategoryDao.getFamilyDeviceCategorys (mCurrFamily.getFamilyId());

        mAdapter.refreshData(groups);
//        setCurrItem();
    }


   /* private void setCurrItem() {
        List<DeviceCategory> deviceCategoryList = mAdapter.getAllData();
        if (deviceCategoryList == null || deviceCategoryList.size() < 1) {
            mGroupPosition = 0;
            mGroup = null;
            AppManager.selectGroup=null;
            return;
        }
        for (DeviceCategory group : deviceCategoryList) {
            group.setChecked(false);
        }

        mGroup = deviceCategoryList.get(mGroupPosition);
        mGroup.setChecked(true);
        mAdapter.notifyDataSetChanged();

        if (mGroup.getCategory() == 1) {
            mBallLight = mBallLightDao.getByUuid(mGroup.getUuid());
        } else if (mGroup.getCategory() == 2) {
            mFan = mFanDao.getByUuid(mGroup.getUuid());
        } else if (mGroup.getCategory() == 3) {
            mFanLamp = mFanLampDao.getByUuid(mGroup.getUuid());
        }

    }*/

    private void initRecycleView() {
        Context context = getContext();
        mAdapter = new DeviceAdapter(context);
        mRecyclerView.setAdapter(mAdapter);
        mLayoutManager = new LinearLayoutManager(context);
        mRecyclerView.setLayoutManager(mLayoutManager);
       /* mAdapter.setOnItemClickListener(position -> {
            mGroupPosition = position;
            setCurrItem();
        });*/


        mAdapter.setItemOperCallBack(new DeviceAdapter.ItemOperCallBack() {

            @Override
            public void notifyAdd(int position) {
                confirmType=1;
                mGroup = mAdapter.getItem(position);
               /* if (mGroup.getCategory() == 1) {
                    mBallLight = mBallLightDao.getByUuid(mGroup.getUuid());
                } else if (mGroup.getCategory() == 2) {
                    mFan = mFanDao.getByUuid(mGroup.getUuid());
                } else if (mGroup.getCategory() == 3) {
                    mFanLamp = mFanLampDao.getByUuid(mGroup.getUuid());
                }*/

                String msg = getString(R.string.sure_add) + mGroup.getName() + getString(R.string.open_before_add);
                showConfirmDialog(msg);
            }

            @Override
            public void notifyDelete(int position) {
                confirmType=2;
                mGroup = mAdapter.getItem(position);
                /*if (mGroup.getCategory() == 1) {
                    mBallLight = mBallLightDao.getByUuid(mGroup.getUuid());
                } else if (mGroup.getCategory() == 2) {
                    mFan = mFanDao.getByUuid(mGroup.getUuid());
                } else if (mGroup.getCategory() == 3) {
                    mFanLamp = mFanLampDao.getByUuid(mGroup.getUuid());
                }else if (mGroup.getCategory() == 4) {
                    mElectricHeater= mElectricHeaterDao.getByUuid(mGroup.getUuid());
                }else if (mGroup.getCategory() == 5) {
                    mBathHeater= mBathHeaterDao.getByUuid(mGroup.getUuid());
                }else if (mGroup.getCategory() == 6) {
                    mCurtain= mCurtainDao.getByUuid(mGroup.getUuid());
                }else if (mGroup.getCategory() == 7) {
                    mClotheshorse= mClotheshorseDao.getByUuid(mGroup.getUuid());
                }else if (mGroup.getCategory() == 8) {
                    mAirConditioning= mAirConditioningDao.getByUuid(mGroup.getUuid());
                }else if (mGroup.getCategory() == 8) {
                    mAirConditioning= mAirConditioningDao.getByUuid(mGroup.getUuid());
                }else if (mGroup.getCategory() == 9) {
                    mHealthBasinp= mHealthBasinDao.getByUuid(mGroup.getUuid());
                }*/

                String msg = getString(R.string.sure_delete) + mGroup.getName() + getString(R.string.open_before_deleting_device);
                showConfirmDialog(msg);
            }

            @Override
            public void notifyForceDelete(int position) {
                confirmType=3;
                mGroup = mAdapter.getItem(position);
                String msg = getString(R.string.sure_force_delete);
                showConfirmDialog(msg);
            }


        });

    }

    @OnClick({R.id.btn_back, R.id.tv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                doBack();
                break;
            case R.id.tv_back:
                doBack();
                break;
        }
    }

    @Override
    protected void confirmOperation() {
        if (confirmType==1){//设置组
            if (mGroup.getCategory() == 1) {
                BallLightDao ballLightDao = new BallLightDao(getContext());
                mBallLight = ballLightDao.getByUuid(mGroup.getUuid());
                AppManager.getInstance(mainActivity).notifyAddGroup(mBallLight);
            } else if (mGroup.getCategory() == 2) {
                String data2 = intToHex(mCurrFamily.getSeed()) + numToHexs(mCurrFamily.getFeedback());
                AppManager.getInstance(mainActivity).commandAdvertise("05","12345678",mGroup.getGroupId(),mGroup.getFamilyId(),data2,73,4777,1,"7202");
            } else if (mGroup.getCategory() == 3) {
                String data2 = intToHex(mCurrFamily.getSeed()) + numToHexs(mCurrFamily.getFeedback());
                AppManager.getInstance(mainActivity).commandAdvertise("05","12345678",mGroup.getGroupId(),mGroup.getFamilyId(),data2,73,4777,1,"7203");
            }else if (mGroup.getCategory() == 4) {
                String data2 = intToHex(mCurrFamily.getSeed()) + numToHexs(mCurrFamily.getFeedback());
                AppManager.getInstance(mainActivity).commandAdvertise("05","12345678",mGroup.getGroupId(),mGroup.getFamilyId(),data2,73,4777,1,"7204");
            }else if (mGroup.getCategory() == 5) {
                String data2 = intToHex(mCurrFamily.getSeed()) + numToHexs(mCurrFamily.getFeedback());
                AppManager.getInstance(mainActivity).commandAdvertise("05","12345678",mGroup.getGroupId(),mGroup.getFamilyId(),data2,73,4777,1,"7205");
            /*}else if (mGroup.getCategory() == 6) {
                String data2 = intToHex(mCurrFamily.getSeed()) + numToHexs(mCurrFamily.getFeedback());
                getAppManager().commandAdvertise("05","12345678",mGroup.getGroupId(),mGroup.getFamilyId(),data2,73,4777,1,"7206");
            }else if (mGroup.getCategory() == 7) {
                String data2 = intToHex(mCurrFamily.getSeed()) + numToHexs(mCurrFamily.getFeedback());
                getAppManager().commandAdvertise("05","12345678",mGroup.getGroupId(),mGroup.getFamilyId(),data2,73,4777,1,"7207");
            }else if (mGroup.getCategory() == 8) {
                String data2 = intToHex(mCurrFamily.getSeed()) + numToHexs(mCurrFamily.getFeedback());
                getAppManager().commandAdvertise("05","12345678",mGroup.getGroupId(),mGroup.getFamilyId(),data2,73,4777,1,"7208");*/
            }else if (mGroup.getCategory() == 9) {
                String data2 = intToHex(mCurrFamily.getSeed()) + numToHexs(mCurrFamily.getFeedback());
                AppManager.getInstance(mainActivity).commandAdvertise("05","12345678",mGroup.getGroupId(),mGroup.getFamilyId(),data2,73,4777,1,"7209");
            }/*else if (mGroup.getCategory() == 11) {
                String data2 = intToHex(mCurrFamily.getSeed()) + numToHexs(mCurrFamily.getFeedback());
                getAppManager().commandAdvertise("05","12345678",mGroup.getGroupId(),mGroup.getFamilyId(),data2,73,4777,1,"720B");
            }*/

        }else if (confirmType==2){//删除组
            if (mGroup.getCategory() == 1) {
                BallLightDao ballLightDao = new BallLightDao(getContext());
                mBallLight = ballLightDao.getByUuid(mGroup.getUuid());
                AppManager.getInstance(mainActivity).notifyDeleteGroup(mBallLight);
            } else if (mGroup.getCategory() == 2) {
                AppManager.getInstance(mainActivity).commandAdvertise("06",mGroup.getFamilyId(),mGroup.getGroupId(),"00000000","000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"7202");
            } else if (mGroup.getCategory() == 3) {
                AppManager.getInstance(mainActivity).commandAdvertise("06",mGroup.getFamilyId(),mGroup.getGroupId(),"00000000","000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"7203");
            }else if (mGroup.getCategory() == 4) {
                AppManager.getInstance(mainActivity).commandAdvertise("06",mGroup.getFamilyId(),mGroup.getGroupId(),"00000000","000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"7204");
            }else if (mGroup.getCategory() == 5) {
                AppManager.getInstance(mainActivity).commandAdvertise("06",mGroup.getFamilyId(),mGroup.getGroupId(),"00000000","000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"7205");
           /* }else if (mGroup.getCategory() == 6) {
                getAppManager().commandAdvertise("06",mGroup.getFamilyId(),mGroup.getGroupId(),"00000000","000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"7206");
            }else if (mGroup.getCategory() == 7) {
                getAppManager().commandAdvertise("06",mGroup.getFamilyId(),mGroup.getGroupId(),"00000000","000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"7207");
            }else if (mGroup.getCategory() == 8) {
                getAppManager().commandAdvertise("06",mGroup.getFamilyId(),mGroup.getGroupId(),"00000000","000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"7208");*/
            }else if (mGroup.getCategory() == 9) {
                AppManager.getInstance(mainActivity).commandAdvertise("06",mGroup.getFamilyId(),mGroup.getGroupId(),"00000000","000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"7209");
            }/*else if (mGroup.getCategory() == 11) {
                getAppManager().commandAdvertise("06",mGroup.getFamilyId(),mGroup.getGroupId(),"00000000","000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"720B");
            }*/

        } else if (confirmType == 3) {
            if (mGroup.getCategory() == 1) {
                AppManager.getInstance(mainActivity).commandAdvertise("F0","12345678","01","00000000","000001",73,4777,2,"7201");
            } else if (mGroup.getCategory() == 2) {
                AppManager.getInstance(mainActivity).commandAdvertise("F0","12345678","01","00000000","000002",73,4777,2,"7202");
            } else if (mGroup.getCategory() == 3) {
                AppManager.getInstance(mainActivity).commandAdvertise("F0","12345678","01","00000000","000003",73,4777,2,"7203");
            }else if (mGroup.getCategory() == 4) {
                AppManager.getInstance(mainActivity).commandAdvertise("F0","12345678","01","00000000","000004",73,4777,2,"7204");
            }else if (mGroup.getCategory() == 5) {
                AppManager.getInstance(mainActivity).commandAdvertise("F0","12345678","01","00000000","000005",73,4777,2,"7205");
           /* }else if (mGroup.getCategory() == 6) {
                getAppManager().commandAdvertise("F0","12345678","01","00000000","000006",73,mCurrFamily.getFeedback(),2,"7206");
            }else if (mGroup.getCategory() == 7) {
                getAppManager().commandAdvertise("F0","12345678","01","00000000","000007",73,mCurrFamily.getFeedback(),2,"7207");
            }else if (mGroup.getCategory() == 8) {
                getAppManager().commandAdvertise("F0","12345678","01","00000000","000008",73,mCurrFamily.getFeedback(),2,"7208");*/
            }else if (mGroup.getCategory() == 9) {
                AppManager.getInstance(mainActivity).commandAdvertise("F0","12345678","01","00000000","000009",73,4777,2,"7209");
            }/*else if (mGroup.getCategory() == 11) {
                getAppManager().commandAdvertise("F0","12345678","01","00000000","00000B",73,4777,2,"720B");
            }*/
        }

    }

}
